package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.AdminInfoMapper;
import com.leased.pojo.AdminInfo;
import com.leased.pojo.AdminInfoExample;
import com.leased.request.BaseQuery;
import com.leased.service.AdminInfoService;
import com.leased.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by shencx on 2017/7/27.
 */
@Service
public class AdminInfoServiceImpl implements AdminInfoService{

    Logger LOG = LoggerFactory.getLogger(AdminInfoServiceImpl.class);

    @Resource
    private AdminInfoMapper adminInfoMapper;

    /**
     * 管理员登录
     * @param adminInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public AdminInfo fetchAdminInfoLogin(AdminInfo adminInfo) throws DatabaseException {
        try {
            if (adminInfo == null){
                LOG.error("fetchAdminInfoLogin 信息为空",adminInfo);
                return null;
            }
            AdminInfoExample example = new AdminInfoExample();
            AdminInfoExample.Criteria criteria = example.createCriteria();
            criteria.andUserNameEqualTo(adminInfo.getUserName());
            criteria.andPasswordEqualTo(adminInfo.getPassword());
            criteria.andIsUsedEqualTo(1);
            List<AdminInfo> list = adminInfoMapper.selectByExample(example);
            if (list == null || list.size() == 0){
                return null;
            }else {
                return list.get(0);
            }
        } catch (Throwable e) {
            LOG.error("fetchAdminInfoLogin 发生异常",e);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取管理员列表
     * @param baseQuery
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<AdminInfo> fetchAdminInfoList(BaseQuery baseQuery) throws DatabaseException {
        try {
            if (baseQuery == null){
                LOG.error("fetchAdminInfoList 信息为空",baseQuery);
                return null;
            }
            AdminInfoExample example = new AdminInfoExample();
            example.setOrderByClause("create_time desc");
            PageMybatisInterceptor.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());
            adminInfoMapper.selectByExample(example);
            Page<AdminInfo> page = PageMybatisInterceptor.endPage();
            return page;
        } catch (Throwable e) {
            LOG.error("fetchAdminInfoList 发生异常",e);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 添加管理员
     * @param adminInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addAdminInfo(AdminInfo adminInfo) throws DatabaseException {
        try {
            if (adminInfo == null){
                LOG.error("addAdminInfo 信息为空",adminInfo);
                return Integer.valueOf(0);
            }
            adminInfo.setCreateTime(new Date());
            adminInfo.setIsUsed(1);
            int val = adminInfoMapper.insertSelective(adminInfo);
            if (val > 0){
                return adminInfo.getId();
            }else {
                return Integer.valueOf(0);
            }
        } catch (Throwable e) {
            LOG.error("addAdminInfo 发生异常",e);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 更新管理员信息与状态
     * @param adminInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public boolean updateAdminInfo(AdminInfo adminInfo) throws DatabaseException {
        try {
            if (adminInfo == null){
                LOG.error("updateAdminInfo 信息为空",adminInfo);
                return false;
            }
            Integer val = adminInfoMapper.updateByPrimaryKeySelective(adminInfo);
            return val>0?true:false;
        }  catch (Throwable e) {
            LOG.error("updateAdminInfo 发生异常",e);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 根据主键获取详情
     * @param id
     * @return
     * @throws DatabaseException
     */
    @Override
    public AdminInfo fetchAdminInfoById(Integer id) throws DatabaseException {
        try {
            if (id == null){
                LOG.error("fetchAdminInfoById 主键为空",id);
                return null;
            }
            AdminInfo adminInfo = adminInfoMapper.selectByPrimaryKey(id);
            return adminInfo;
        } catch (Throwable e) {
            LOG.error("fetchAdminInfoById 发生异常",e,id);
            throw new DatabaseException(e.getMessage());
        }
    }
}
